package com.bolt.support.spring.jpa.jpql;

import com.bolt.support.query.conditions.domain.SqlClause;

import java.util.ArrayList;
import java.util.List;

public abstract class SqlFuncClause<T, Children> implements SqlClause<T> {

    protected final static Templates templates = new Templates();

    protected Ops ops;

    protected List<String> args = new ArrayList<>();

    public Children toChar(String patten) {
        ops = Ops.TOCHAR;
        args.add(patten);
        return (Children) this;
    }

    public Children concat(String str) {
        ops = Ops.CONCAT;
        args.add(str);
        return (Children) this;
    }
    public Children group_concat() {
        ops = Ops.GROUP_CONCAT;
        return (Children) this;
    }

    public Children wm_concat() {
        ops = Ops.WM_CONCAT;
        return (Children) this;
    }


    public Children substring(int start, int end) {
        ops = Ops.SUBSTR;
        args.add(String.valueOf(start));
        args.add(String.valueOf(end));
        return (Children) this;
    }

    public Children trim() {
        ops = Ops.TRIM;
        return (Children) this;
    }

    public Children lower() {
        ops = Ops.LOWER;
        return (Children) this;
    }

    public Children upper() {
        ops = Ops.UPPER;
        return (Children) this;
    }

    public Children length() {
        ops = Ops.LENGTH;
        return (Children) this;
    }


    public Children max() {
        ops = Ops.MAX;
        return (Children) this;
    }

    public Children min() {
        ops = Ops.MIN;
        return (Children) this;
    }

    public Children sum() {
        ops = Ops.SUM;
        return (Children) this;
    }

    public Children count() {
        ops = Ops.COUNT;
        return (Children) this;
    }

    public Children avg() {
        ops = Ops.AVG;
        return (Children) this;
    }


    //绝对值
    public Children abs() {
        ops = Ops.ABS;
        return (Children) this;
    }

    //平方根
    public Children sqrt() {
        ops = Ops.SQRT;
        return (Children) this;
    }

    //取余数
    public Children mod() {
        ops = Ops.MOD;
        return (Children) this;
    }


    //取集合的数量
    public Children size() {
        ops = Ops.SIZE;
        return (Children) this;
    }

    public abstract  Children as(String alias);



    public OrderClause asc() {
        return new OrderClause(SqlOrder.ASC, this);
    }

    public OrderClause desc() {
        return new OrderClause(SqlOrder.DESC, this);
    }
}

 